﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Core.CMS.ServerControls
{
    public class ContentArea : UserControl, INamingContainer
    {
        ContentItem contentItem = null;
        string content = null;
        ITemplate edit = null;
        ITemplate normal = null;

        public ContentItem ContentItem
        {
            get {
                EnsureChildControls();
                return contentItem; 
            }
        }

        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        public bool EnableEditing
        {
            get { return (bool)ViewState["edit"]; }
            set { ViewState["edit"] = value; }
        }

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), Description("Editable content template"), TemplateContainer(typeof(ContentItem))]
        public ITemplate EditTemplate
        {
            get { return edit; }
            set { edit = value; }
        }
        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), Description("Read only content template"), TemplateContainer(typeof(ContentItem))]
        public ITemplate ViewTemplate
        {
            get { return normal; }
            set { normal = value; }
        }

        public override void DataBind()
        {
           // Create the child controls...
           CreateChildControls();
           // mark that the children have been created
           this.ChildControlsCreated = true;
           // call the DataBind method
           base.DataBind ();      
        }

        public ContentArea()
        {
            if (ViewState["edit"] == null)
                ViewState["edit"] = false;
            if (ViewState["name"] == null)
                ViewState["name"] = "";
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            contentItem = new ContentItem();
            contentItem.Content = content;

            if (EnableEditing && EditTemplate != null)
                EditTemplate.InstantiateIn(contentItem);
            else if (ViewTemplate != null)
                ViewTemplate.InstantiateIn(contentItem);

            Controls.Add(contentItem);
        }
    }

    public class ContentItem : WebControl, INamingContainer
    {
        string content = "";

        public PageInfo PageInfo
        {
            get
            {
                if (Page.RouteData.Values["ID"] != null)
                    return PageInfo.Get(int.Parse(Page.RouteData.Values["ID"].ToString()));
                else if (Page.Request.QueryString["ID"] != null)
                    return PageInfo.Get(int.Parse(Page.Request.QueryString["ID"]));

                return null;
            }
        }

        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        public ContentItem()
        {

        }
    }
}
